Skip to content

fix(chat): preserve #file reference when editing text before it - #333965

Open
Anupam Kumar (AnupamKumar-1) wants to merge 6 commits into
microsoft:mainfrom
AnupamKumar-1:fix/preserve-file-reference-before-edit
Open

fix(chat): preserve #file reference when editing text before it#333965
Anupam Kumar (AnupamKumar-1) wants to merge 6 commits into
microsoft:mainfrom
AnupamKumar-1:fix/preserve-file-reference-before-edit

Conversation

@AnupamKumar-1

Copy link
Copy Markdown

Fixes #333941

Editing text before an inline #file: reference (for example, pressing
Backspace immediately before it or retyping an earlier word) could delete
the reference entirely.

The existing recovery logic handled replacements that enclosed the reference,
but did not preserve the reference when unrelated edits changed its offset.
Such edits could therefore fall through to the removal path.

This adds a touchesReference check to distinguish edits that overlap the
reference from edits that occur outside it. For non-overlapping edits, the
reference range is recomputed from the net offset delta instead of removing
the reference. Edits that touch the reference continue to remove it.

Added regression tests covering deletion before the reference, replacement
before it, deletion touching its first character, and edits both before and
after the reference in a single change event.

Manually verified the behavior with a real #file: reference in a running
dev build.

How to test

  1. Open Chat.
  2. Type a sentence, e.g. Compare the ownership section of .
  3. Type #, pick a file so #file:README.md is inserted inline, then continue typing with the current draft.
  4. Move the caret before the reference and edit there — e.g. press Backspace immediately before it, or retype a word right in front of it.
  5. Confirm the #file:README.md reference is preserved instead of being deleted.

Also covered by the added tests in chatVariables.test.ts (delete-before, replace-before, delete-touching-first-char, edits-before-and-after).

Copilot AI balanced review requested due to automatic review settings September 2, 2026 10:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Enclosing replacements can incorrectly trigger a second edit that deletes unrelated text.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Preserves inline chat references when unrelated edits shift their offsets.

Changes:

  • Detects whether edits overlap a reference.
  • Recomputes ranges after non-overlapping edits.
  • Adds regression tests for related editing scenarios.
File summaries
File Description
chatDynamicVariables.ts Adds overlap detection and range recovery.
chatVariables.test.ts Adds reference-editing regression tests.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/vs/workbench/contrib/chat/browser/attachments/chatDynamicVariables.ts Outdated
@AnupamKumar-1
Anupam Kumar (AnupamKumar-1) force-pushed the fix/preserve-file-reference-before-edit branch from 82e9a7d to 21dda2f Compare September 2, 2026 10:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The range-tracking fix is focused and adequately covered by regression tests.

Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Chat: editing text before an inline #file: reference deletes the reference

3 participants